package com.atguigu.ssyx.search.controller;

import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.search.SkuEs;
import com.atguigu.ssyx.search.service.SkuService;
import com.atguigu.ssyx.vo.search.SkuEsQueryVo;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 商品搜索列表接口
 * </p>
 */
@RestController
@RequestMapping("api/search/sku")
@RequiredArgsConstructor
public class SkuApiController {
    private final SkuService skuService;


    @ApiOperation(value = "上架商品")
    @GetMapping("inner/upperSku/{skuId}")
    public Result upperGoods(@PathVariable("skuId") Long skuId) {
        skuService.upperSku(skuId);
        return Result.ok(null);
    }

    @ApiOperation(value = "下架商品")
    @GetMapping("inner/lowerSku/{skuId}")
    public Result lowerGoods(@PathVariable("skuId") Long skuId) {
        skuService.lowerSku(skuId);
        return Result.ok(null);
    }

    @ApiOperation(value = "根据id删除商品")
    @GetMapping("inner/deleteSku/{skuId}")
    public Result deleteGoods(@PathVariable("skuId") Long skuId) {
        skuService.deleteSku(skuId);
        return Result.ok(null);
    }

    @ApiOperation(value = "获取爆款商品")
    @GetMapping("inner/findHotSkuList")
    public List<SkuEs> findHotSkuList() {
        List<SkuEs> hotSkuList = skuService.findHotSkuList();
        return hotSkuList;
    }


    @ApiOperation("查询分类下的商品")
    @GetMapping("{page}/{limit}")
    public Result list(
            @PathVariable("page") Integer page,
            @PathVariable("limit") Integer limit,
            SkuEsQueryVo skuEsQueryVo) {


        //从第一页开始
        PageRequest pageRequest = PageRequest.of(page - 1, limit);
        Page<SkuEs> p = skuService.search(pageRequest, skuEsQueryVo);
        return Result.ok(p);
    }


    @ApiOperation("更新商品热度")
    @GetMapping("inner/updateHotSku/{skuId}")
    public Boolean updateHotSku(@PathVariable("skuId") Long skuId) {
        skuService.updateHotSku(skuId);

        return true;
    }

}